<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

use think\Config;
use think\Session;
use fast\Http;

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;


class Awstest extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();

    }

    //http://fastaws.test/api/awstest/test_s3
    public function test_s3()
    {
        //Create an Amazon S3 client
        $s3 = new S3Client([
            'version' => 'latest',
            'region'  => 'us-west-2'
        ]);


        try {
            //获取bucket列表
//            $result = $s3->listBuckets();
//            foreach($result['Buckets'] as $b) {
//                var_dump($b);
//            }

            //上传文件
            $s3->putObject([
                'Bucket' => 'my-bucket',
                'Key'    => 'my-object',
                'Body'   => fopen('robots.txt', 'r'),
                'ACL'    => 'public-read',
            ]);

            //上传obj
            $bucket = 'my-bucket';
            $result = $s3->putObject(array(
                'Bucket' => $bucket,
                'Key' => '201711/404.html',
                'SourceFile' => './404.html',
                'Metadata' => array(
                    'Foo' => 'abc',
                    'Bar' => '123'
                )
            ));

            //copy
            $result = $s3->copyObject(array(
                'Bucket' => $bucket,
                'CopySource' => urlencode($bucket . '/' . '201711/404.html'),
                'Key' => '201711/606.html'
            ));

            //获取obj列表
            $iterator = $s3->getIterator('ListObjects', array('Bucket' => $bucket));
            foreach ($iterator as $object) {
                //if(strpos($object['Key'], 'log/')!== false){
                /* $client->deleteObject([
                    'Bucket' => $bucket,
                    'Key'    => $object['Key']
                ]); */
                echo $object['Key'] . "\n";
                //}
            }


        } catch (S3Exception $e) {
            echo "There was an error uploading the file.\n";
        }

        echo "test!";
    }

    //http://fastaws.test/api/awstest/phpinfo
    public function phpinfo()
    {
        
        phpinfo();
    }


    //http://fastaws.test/api/awstest/redistest
    public function redistest()
    {
        $redis = new \Redis();
//        $redis->connect('127.0.0.1', 6379);//http://220.132.102.45/
        $redis->connect('220.132.102.45', 6379); //redis尽量只使用本地访问, 多源复制还是得用远程redis；最好是局域网192.168
        $redis->auth('mirohome');
        echo "Connection to server successfully";
        echo "<br>Server is running: " . $redis->ping();
//存储数据到列表中
        $redis->lpush("tutorial-list", "11");
        $redis->lpush("tutorial-list", "22");
        $redis->lpush("tutorial-list", "33");
// 获取存储的数据并输出
        $arList = $redis->lrange("tutorial-list", 0 ,50);

        // 获取列表长度
        $llen = $redis->llen('tutorial-list');
        echo "<br>".$llen;
//        while ($llen){
//            echo "弹出数据：".$redis->lpop('tutorial-list')."<br/>";
//            $llen = $redis->llen('tutorial-list');
//        }

        echo "<br>";
        echo "".json_encode($arList);
        echo "<br>";
        var_export($arList);

        $redis->set("tutorial-name", "AAA");
        echo "<br>Stored string in redis:: " . $redis->get("tutorial-name");


        //显示redis存储的字段，list如果lpop完，这个字段就没有了
        $kList = $redis->keys("*");
        echo "<br>Stored keys in redis:: ";
        print_r($kList);
    }

    //http://fastaws.test/api/awstest/testA
    //测试主从复制，读写分离
    public function testA() {

        $test = \app\admin\model\ms\Test::get(1);

        echo "testA<br>".$test->title;//读从库

        $test2 = \app\admin\model\ms\Test::get(2);
        $test2->title = "首都发生的";
        $test2->save();//写主库
    }


}





